package com.wish.g1.house.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.wish.g1.common.redis.service.RedisService;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.house.entity.Region;
import com.wish.g1.house.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地区表 前端控制器
 * </p>
 *
 * @author Dragon
 * @since 2022-11-18
 */
@RestController
@RequestMapping("/region")
public class RegionController{

    @Autowired
    private IRegionService regionService;
    @Autowired
    private RedisTemplate redisTemplate;

    // redis缓存省市区数据
    @PostConstruct
    public void regionList(){
        if (redisTemplate.hasKey("regionList")){
            return;
        }
        List<Region> list = regionService.list();
        redisTemplate.opsForList().leftPushAll("regionList",list);
    }

    // 获取redis缓存的省市县数据
    @GetMapping("/getRegionList")
    public List<Region> getRegionList(){
        return redisTemplate.opsForList().range("regionList",0,-1);
    }

    /**
     * 查询 省 市 区
     * @param id
     * @return
     */
    @GetMapping("/queryRegionListById/{id}")
    public List<Region> queryRegionListById(@PathVariable("id") Integer id){
        ArrayList<Region> list = new ArrayList<>();
        List<Region> list1 = regionService.list();
        for (Region region : list1) {
            if(region.getPid() == id){
                list.add(region);
            }
        }
        return list;
    }

    // 查询省市县name
    @GetMapping("/getNameById/{regionId}")
    public String getNameById(@PathVariable("regionId") Integer regionId) {
        Region region = regionService.getById(regionId);
        return region.getName();
    }

    // 省市县数据
    // @RequiresPermissions("house:region:list")
    @GetMapping("/list")
    public List<Tree<String>> list(){
        List<Region> nodeList = regionService.list();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("id");
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getPid().toString());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("label", treeNode.getName());
                    tree.putExtra("value", treeNode.getId());
                });
        return treeNodes;
    }

}
